<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class NearbyVideoController extends CommonController
{
    public $model;
    
    public function _initialize(){ 
        parent::_initialize();
        $this->model = M('NearbyVideos');
    }

    public function index(){
        $map = [];
        list($list, $page) = $this->lists($this->model, $map, 'sort desc,id asc');
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['title'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写标题'));
            }
            if(empty($data['video'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传视频'));
            }
            $siteurl = C("SiteUrl");
            $video_cover = '';
            if($data['video']){
                require_once 'ThinkPHP/Library/Vendor/autoload.php';
                $ffmpeg = new \Common\Util\FFMpeg();
                $ffmpeg = $ffmpeg->ffmpeg;
                $cover_path = './'.C('UPLOAD_PATH').'/videocover/';
                if(!is_dir($cover_path)){
                    mkdir($cover_path, 0755, true);
                }
                try {
                    // 视频路径
                    $video_path = str_replace($siteurl, './', $data['video']);
                    if(is_file($video_path)){
                        $poster_path = $cover_path.md5($video_path).'.jpg';// 生成封面保存路径
                        if(!is_file($poster_path)){
                            $video = $ffmpeg->open($video_path);
                            $frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
                            $frame->save($poster_path, false);  
                        }
                        $video_cover = str_replace('/./', '/', $siteurl.$poster_path);
                    }
                } catch (\Exception $e) {
                    trace('生成图片失败:'.$e->getMessage(), 'error');
                }
            }
            $data['cover'] = $video_cover;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $id = $this->model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $sort = $this->model->max('sort')+1;
            $this->assign('sort', $sort);
            $this->display();
        }
    }

    public function edit(){
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            if(empty($data['title'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写标题'));
            }
            if(empty($data['video'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传视频'));
            }
            $siteurl = C("SiteUrl");
            $video_cover = '';
            if($data['video']){
                require_once 'ThinkPHP/Library/Vendor/autoload.php';
                $ffmpeg = new \Common\Util\FFMpeg();
                $ffmpeg = $ffmpeg->ffmpeg;
                $cover_path = './'.C('UPLOAD_PATH').'/videocover/';
                if(!is_dir($cover_path)){
                    mkdir($cover_path, 0755, true);
                }
                try {
                    // 视频路径
                    $video_path = str_replace($siteurl, './', $data['video']);
                    if(is_file($video_path)){
                        $poster_path = $cover_path.md5($video_path).'.jpg';// 生成封面保存路径
                        if(!is_file($poster_path)){
                            $video = $ffmpeg->open($video_path);
                            $frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
                            $frame->save($poster_path, false);  
                        }
                        $video_cover = str_replace('/./', '/', $siteurl.$poster_path);
                    }
                } catch (\Exception $e) {
                    trace('生成图片失败:'.$e->getMessage(), 'error');
                }
            }
            $data['cover'] = $video_cover;
            $data['update_time'] = time();
            if($this->model->create($data)){
                $res = $this->model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'修改成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'修改失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>$this->model->getError()]);
            }
        } else {
            $info = $this->model->where(['id' => $id])->find();
            if(empty($info)){
                $this->error('信息不存在');
            }
            $info['city'] = implode('/', [$info['pro'],$info['city'],$info['area']]);
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = $this->model->where(['id' => $id])->delete();
        if ($res) {
            $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
        } else {
            $this->ajaxReturn(array('s' => false, 'm' => '删除失败!'));
        }
    }
}